package com.yiwei.ydd.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.text.Spanned;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.yiwei.ydd.R;
import com.yiwei.ydd.api.model.CouponListModel;
import com.yiwei.ydd.util.DateTime;
import com.yiwei.ydd.util.Spans;

import butterknife.BindView;
import butterknife.ButterKnife;


/**
 * Created by xsooy-pc on 2017/10/23.
 */

public class CouponSelectAdapter extends ItemAdapter<CouponListModel.DatasBean, CouponSelectAdapter.DefaultHolder> {


    private final int x60;
    private final int x30;
    private int type;
    private Context context;

    public CouponSelectAdapter(Context context, int type) {
        super(context);
        this.context = context;
        x60 = context.getResources().getDimensionPixelSize(R.dimen.x60);
        x30 = context.getResources().getDimensionPixelSize(R.dimen.x30);
        this.type = type;
    }

    @Override
    public DefaultHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new DefaultHolder(inflate(R.layout.item_coupon_select, parent));
    }

    @Override
    public void onBindViewHolder(DefaultHolder holder, int position) {
        CouponListModel.DatasBean item = getItem(position);
        switch (type) {
            case 0:
                holder.layoutTotal.setBackgroundResource(R.mipmap.img_youhui_use_bg);

                break;
            case 1:
                holder.layoutTotal.setBackgroundResource(R.mipmap.img_youhui_used_bg);
                holder.txtTitle.setTextColor(0xff999999);
                holder.txtContent.setTextColor(0xff999999);
                break;
            case 2:
                holder.layoutTotal.setBackgroundResource(R.mipmap.img_youhui_used_bg);
                holder.txtTitle.setTextColor(0xff999999);
                holder.txtContent.setTextColor(0xff999999);
                break;
        }
        holder.txtTitle.setText(item.title);
        holder.txtContent.setText(item.limit_desc);
        holder.txtUseDesc.setText(item.use_desc);

        Spanned name = Spans.with(context).font(item.par, x60, 0xffFFFFFF).
                font("\n立即使用", x30, 0xffFFFFFF).
                build();
        holder.txtMoney.setText(name);
//        有效期 2018.04.08 00:00:00 - 2018.07.04 23:59:59
        holder.txtTime.setText("有效时间" + DateTime.toDateTime(item.start_date * 1000) + "-" + DateTime.toDateTime(item.end_date * 1000));
        if (item.isCheck) {
            holder.imgStatus.setImageResource(R.mipmap.img_coupon_check);
        } else {
            holder.imgStatus.setImageResource(R.mipmap.img_oil_change_uncheck);
        }


        holder.layoutTotal.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onChooseListener != null) {
                    onChooseListener.onClick(item.par, item.id);
                }
                for (int i = 0; i < getItemCount(); i++) {
                    if (i == position) {
                        getmSource().get(i).isCheck = true;
                    } else {
                        getmSource().get(i).isCheck = false;
                    }
                    notifyDataSetChanged();
                }
            }
        });

    }

    class DefaultHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.txt_money)
        TextView txtMoney;
        @BindView(R.id.txt_title)
        TextView txtTitle;
        @BindView(R.id.txt_content)
        TextView txtContent;
        @BindView(R.id.txt_time)
        TextView txtTime;
        @BindView(R.id.layout_total)
        LinearLayout layoutTotal;
        @BindView(R.id.txt_use_desc)
        TextView txtUseDesc;
        @BindView(R.id.img_status)
        ImageView imgStatus;

        public DefaultHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    private OnChooseListener onChooseListener;

    public void setOnChooseListener(OnChooseListener onChooseListener) {
        this.onChooseListener = onChooseListener;
    }

    public interface OnChooseListener {
        void onClick(String money, String id);
    }
}
